<template>
	<view>
		<!-- #ifndef MP-ALIPAY -->
		<nav-bar title="设置"></nav-bar>
		<!-- #endif -->
		<public-module></public-module>
		<u-gap height="30"></u-gap>
		<u-cell-group :border="false">
			<u-cell-item title="头像" :center="true" @click="chooseAvatar">
				<u-avatar :src="model.avatarUrl" mode="circle" style="vertical-align: -15rpx;"></u-avatar>
			</u-cell-item>
		</u-cell-group>
		<view class="set_form">
			<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
				<u-form-item :label-position="labelPosition" label="姓名" prop="name">
					<u-input :border="border" placeholder="请输入姓名" v-model="model.name" type="text"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="性别" prop="sex">
					<u-input :border="border" type="select" :select-open="actionSheetShow" v-model="model.sex" placeholder="请选择性别" @click="actionSheetShow = true"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="所在地区" prop="region" label-width="150">
					<u-input :border="border" type="select" :select-open="pickerShow" v-model="model.region" placeholder="请选择地区" @click="pickerShow = true"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" :border-bottom="false" label="简介" prop="intro">
					<u-input type="textarea" :border="border" placeholder="请填写简介" v-model="model.intro" />
				</u-form-item>				
			</u-form>
		</view>
		
		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item title="联系方式" :value="model.phone" @click="onPageJump('../myUni/bindMobile')"></u-cell-item>
				<!-- #ifdef APP-PLUS -->
				<u-cell-item title="版本" :value="versionName"></u-cell-item>
				<!-- #endif -->
			</u-cell-group>
		</view>
		
		<u-gap height="30"></u-gap>
		<view class="agreement">
			<u-checkbox v-model="check" @change="checkboxChange"></u-checkbox>
			<view class="agreement-text">
				勾选代表同意<view class="coler-primary">《uView》</view>的版权协议
			</view>
		</view>
		<view class="u-m-30">
			<u-button shape="square" type="success" @click="submit">提交</u-button>
		</view>
		<view class="u-m-30">
			<u-button shape="square" type="error" @click="loginOut">退出登录</u-button>
		</view>
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-picker mode="region" v-model="pickerShow" @confirm="regionConfirm"></u-picker>
		
	</view>
</template>

<script>
// #ifdef APP-PLUS
import APPUpdate, { getCurrentNo } from '@/plugins/APPUpdate';
// #endif
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		let that = this;
		return {
			versionName: "",
			model: {
				avatarUrl:'',
				name: '',
				sex: '',
				region: '',
				intro: '',
				phone: '未绑定'
			},
			rules: {
				name: [
					{
						required: true,
						message: '请输入姓名',
						trigger: 'blur' ,
					},
					{
						min: 2,
						max: 5,
						message: '姓名长度在3到5个字符',
						trigger: ['change','blur'],
					},
					{
						// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return this.$u.test.chinese(value);
						},
						message: '姓名必须为中文',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change','blur'],
					},
					// 异步验证，用途：比如用户注册时输入完账号，后端检查账号是否已存在
					// {
					// 	trigger: ['blur'],
					// 	// 异步验证需要通过调用callback()，并且在里面抛出new Error()
					// 	// 抛出的内容为需要提示的信息，和其他方式的message属性的提示一样
					// 	asyncValidator: (rule, value, callback) => {
					// 		this.$u.post('/ebapi/public_api/index').then(res => {
					// 			// 如果验证出错，需要在callback()抛出new Error('错误提示信息')
					// 			if(res.error) {
					// 				callback(new Error('姓名重复'));
					// 			} else {
					// 				// 如果没有错误，也要执行callback()回调
					// 				callback();
					// 			}
					// 		})
					// 	},
					// }
				],
				sex: [
					{
						required: true,
						message: '请选择性别',
						trigger: 'change'
					},
				],
				intro: [
					{
						required: true,
						message: '请填写简介'
					},
					{
						min: 5,
						message: '简介不能少于5个字',
						trigger: 'change' ,
					},
					// 正则校验示例，此处用正则校验是否中文，此处仅为示例，因为uView有this.$u.test.chinese可以判断是否中文
					{
						pattern: /^[\u4e00-\u9fa5]+$/gi,
						message: '简介只能为中文',
						trigger: 'change',
					},
				],
				region: [
					{
						required: true,
						message: '请选择地区',
						trigger: 'change',
					}
				],
			},
			check: false,
			border: false,
			actionSheetList: [
				{
					text: '男'
				},
				{
					text: '女'
				},
				{
					text: '保密'
				}
			],
			actionSheetShow: false,
			pickerShow: false,
			selectShow: false,
			radioCheckWidth: 'auto',
			radioCheckWrap: false,
			labelPosition: 'left',
			codeTips: '',
			errorType: ['message'],
		};
	},
	created() {
		let that = this;
		// 监听从裁剪页发布的事件，获得裁剪结果
		uni.$on('uAvatarCropper', path => {
			this.model.avatarUrl = path;
			that.$store.state.userInfo.avatarUrl = path;
			// 可以在此上传到服务端
			// uni.uploadFile({
			// 	url: 'http://www.example.com/upload',
			// 	filePath: path,
			// 	name: 'file',
			// 	complete: (res) => {
			// 		console.log(res);
			// 	}
			// });
		})
	},
	onLoad() {
		this.model.avatarUrl = this.$store.state.userInfo.avatarUrl || "";
		// #ifdef APP-PLUS
		getCurrentNo(res => {
			this.versionName = res.versionName
		});
		// #endif
	},
	computed: {
		borderCurrent() {
			return this.border ? 0 : 1;
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	methods: {
		...mapMutations(['emptyUserInfo']),
		submit() {
			this.$refs.uForm.validate(valid => {
				if (valid) {
					if(!this.model.agreement) return this.$u.toast('请勾选协议');
					console.log('验证通过');
					console.log(this.model);
				} else {
					console.log('验证失败');
				}
			});
		},
		loginOut() {
			this.emptyUserInfo();
            setTimeout(()=>{
                uni.switchTab({
                    url: '../myUni/my'
                });
            },500)
		},
		chooseAvatar() {
			// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
			this.$u.route({
				// 关于此路径，请见下方"注意事项"
				url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
				// 内部已设置以下默认参数值，可不传这些参数
				params: {
					// 输出图片宽度，高等于宽，单位px
					destWidth: 300,
					// 裁剪框宽度，高等于宽，单位px
					rectWidth: 300,
					// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
					fileType: 'jpg',
				}
			})
		},
		// 点击actionSheet回调
		actionSheetCallback(index) {
			uni.hideKeyboard();
			this.model.sex = this.actionSheetList[index].text;
		},
		// 勾选版权协议
		checkboxChange(e) {
			this.model.agreement = e.value;
		},
		// 选择地区回调
		regionConfirm(e) {
			this.model.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
		},
		errorChange(index) {
			if(index == 0) this.errorType = ['message'];
			if(index == 1) this.errorType = ['toast'];
			if(index == 2) this.errorType = ['border-bottom'];
			if(index == 3) this.errorType = ['border'];
		},
		onPageJump(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style scoped lang="scss">
.set_form {
	background-color: #fff;
	padding:0 30rpx;
}

.agreement {
	display: flex;
	align-items: center;
	margin: 40rpx 40rpx 20rpx;

	.agreement-text {
		margin-left: -20rpx;
		color: $u-tips-color;
	}
}

.coler-primary{
	display: inline;
	color: $u-type-primary;
}
</style>
